﻿using System.Windows.Forms;
using System.Windows.Forms.Integration;
using ClassMetadataBrowser.Domain;
using System.Collections.Generic;
using System;
using Gl.SettingsManager;
using ClassMetadataBrowser.ToolControls;
using System.ComponentModel;
using ClassMedataBrowser.Domain;
using System.Diagnostics;

namespace ClassMetadataBrowser
{
	public class HostWindow : SettingsForm, IHost
	{
		private IConsumerForm _currentForm;
		private IMetadataProvider _provider;
		private MembersList _membersList;
		private SolutionFiles _solutionFiles;
		private FocusMode _mode;

		public HostWindow()
		{
			InitializeComponent();
		}

		protected override void OnLoad(EventArgs e)
		{
			Visible = false;
			base.OnLoad(e);
			Visible = true;
		}

		private void InitializeControl(FocusMode mode, IMetadataProvider provider, SolutionFilesSettings settings)
		{
			this.SuspendLayout();

			switch (mode)
			{
				case FocusMode.MembersSearch:
					_currentForm = _membersList;
					_solutionFiles.Visible = false;  
					_membersList.Visible = true;
					break;
				case FocusMode.SolutionFileSearch:
					_currentForm = _solutionFiles;
					_solutionFiles.Visible = true;
					_membersList.Visible = false;
					break;
			}

			Debug.Assert(_currentForm != null);

			CancelButton = _currentForm.CancelButton;
			AcceptButton = _currentForm.OkButton;

			_currentForm.AcceptColumnDescriptor(settings.SolutionFilesColumns);
			_currentForm.Bind(provider);

			this.ResumeLayout(false);
			_currentForm.SetFocusOnCriteria();
		}

		public void Accept(IMetadataProvider provider, FocusMode mode, SolutionFilesSettings settings)
		{
			InitializeControl(mode, provider, settings);

			_mode = mode;
			_provider = provider;
			_provider.Rebind += Rebind;
		}

		public void Rebind(object sender, EventArgs e)
		{
			if (Visible && _mode == FocusMode.MembersSearch)
				this.Invoke(new MetadataLoaded(_membersList.Bind), sender);
		}

		public void Initialized(object sender, EventArgs e)
		{

		}

		public void Clear()
		{
			_membersList.Clear();
		}

		#region IHostWindow Members

		void IHost.Close()
		{
			_provider.Rebind -= Rebind;
			_provider.Initialized -= Initialized;
			this.Close();
		}

		#endregion

		private void InitializeComponent()
		{
			this._membersList = new ClassMetadataBrowser.ToolControls.MembersList();
			this._solutionFiles = new ClassMetadataBrowser.ToolControls.SolutionFiles();
			this.SuspendLayout();
			// 
			// membersList1
			// 
			this._membersList.Dock = System.Windows.Forms.DockStyle.Fill;
			this._membersList.HostWindow = null;
			this._membersList.Location = new System.Drawing.Point(0, 0);
			this._membersList.Name = "membersList1";
			this._membersList.NameColumnWidth = 570;
			this._membersList.SearchCriteria = null;
			this._membersList.Size = new System.Drawing.Size(750, 435);
			this._membersList.TabIndex = 0;
			// 
			// solutionFiles1
			// 
			this._solutionFiles.Dock = System.Windows.Forms.DockStyle.Fill;
			this._solutionFiles.Location = new System.Drawing.Point(0, 0);
			this._solutionFiles.Name = "solutionFiles1";
			this._solutionFiles.Size = new System.Drawing.Size(750, 435);
			this._solutionFiles.TabIndex = 1;
			// 
			// HostWindow
			// 
			this.ClientSize = new System.Drawing.Size(750, 435);
			this.Controls.Add(this._solutionFiles);
			this.Controls.Add(this._membersList);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "HostWindow";
			this.ShowIcon = false;
			this.ShowInTaskbar = false;
			this.KeyPreview = true;

			this.ResumeLayout(false);
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);

			_currentForm.SaveSettings();
			if (DialogResult == DialogResult.OK)
				_currentForm.SelectCurrent(false);
		}

		protected override Settings OnSettingsProviderNeeded()
		{
			return Settings.GetSettingsWithRegistryProvider();
		}
	}
}
